/**
 * @copy 读取静态文件
 *
 */
const http = require('http');
const fs = require('fs');


let server = http.createServer((req, res) => {
    if(req.url.indexOf('/static/') !== - 1) {
        console.log('获取静态文件')
        readFile('.' + req.url).then((data) => {
            res.write(data, "binary");
            res.end();
        })
    } else {
        console.log('其他接口')
        res.write("other");
        res.end();
    }
})

/**
 * 读取文件
 * @param filePath
 * @returns {Promise<unknown>}
 */
function readFile (filePath) {
    return new Promise((resolve, reject) => {
        fs.readFile(filePath, "binary", (err, data) => {
            if(err) {
                reject(err);
                return
            }
            resolve(data)
        })
    })
}

server.listen(1024, () => {
    console.log('服务开启！')
})

//访问：http://localhost:1024/static/1.png
